package io.wdb.handler;

import io.wdb.config.AmqpConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.wdb.exchange.constant.SourceEnum;
import com.wdb.exchange.model.Event;

import java.nio.charset.Charset;

@Component
public class AmqpHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    ActivityListenHandler activityListenHandler;

    @RabbitListener(queues = AmqpConfig.ACTIVITY_SERVICE_QUEUE_NAME, containerFactory = "aRabbitListenerContainerFactory")
    public void processServiceMessage(@Payload byte[] message) {
        try {
    		String msgstr = new String(message, Charset.forName("UTF-8"));
        logger.info("processServiceMessage {}", msgstr);
        Event event = Event.fromJson(msgstr);
        if(!SourceEnum.ACTIVITY.equals(event.source)){
        		activityListenHandler.process(event);
        }
        
    }catch(Exception e) {
    		logger.error(e.getMessage());
    }
    }
}
